package com.uxhao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uxhao.eduservice.client.OrderClient;
import com.uxhao.eduservice.client.UcenterClient;
import com.uxhao.eduservice.entity.EduCourse;
import com.uxhao.eduservice.entity.adminVo.DashboardVo;
import com.uxhao.eduservice.entity.adminVo.LineChartVo;
import com.uxhao.eduservice.service.AdminService;
import com.uxhao.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author uxhao
 * @Description TODO
 * @date 2022/04/03/ 19:55:00
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    OrderClient orderClient;
    @Autowired
    UcenterClient ucenterClient;
    @Autowired
    EduCourseService courseService;

    /**
     * @Description: 管理端面板数据查询
     * @date 2022-04-03 23:47:33
     * @return:
     */
    @Override
    public DashboardVo dashboardData() {
        //远程调用订单服务，查询订单总数和总交易额
        DashboardVo dashboardVo = new DashboardVo();
        DashboardVo dashboardVo1 = orderClient.countAllOrderAndAmount();
        BeanUtils.copyProperties(dashboardVo1, dashboardVo);
        //远程调用会员服务，查询会员总数
        DashboardVo dashboardVo2 = ucenterClient.countAllRegister();
        //BeanUtils.copyProperties(dashboardVo2, dashboardVo); // 不能用这个赋值了，因为除了会员数量其他字段为null。
        dashboardVo.setRegisterNumber(dashboardVo2.getRegisterNumber());
        //查询课程总数
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("is_deleted", 0);
        int courseNumber = courseService.count(courseQueryWrapper);
        dashboardVo.setCourseNumber(courseNumber);
        return dashboardVo;
    }

    /**
     * @Description: 获取折线图数据
     * @date 2022-04-04 17:51:32
     * @return:
     */
    @Override
    public Map<String, Object> getLineChart() {
        //获取订单的数据
        Map<String, Object> orderAmountData = orderClient.countLastAndCurrentOrderAndAmount();
        //获取会员的数据
        Map<String, Object>  memberData = ucenterClient.countLastAndCurrentWeekRegister();
        //获取课程的数据
        Map<String, Object> courseData = courseService.countLastAndCurrentWeekCourse();
        Map<String, Object> map = new HashMap<>();
        map.put("orderAmountData", orderAmountData);
        map.put("memberData", memberData);
        map.put("courseData", courseData);
        return map;
    }

}
